<?php
/*
 * Helper Upload
 *  upload file to dir
 * 
 *	//$uploader->file($_FILES['file'])->sub('subdir')->to(UPLOAD_RAND_NAME)->upload()->getPath();
 */
define ( 'UPLOAD_ORG_NAME', 'USE_ORG_NAME' );
define ( 'UPLOAD_RAND_NAME', 'USE_RAND_NAME' );
define ( 'UPLOAD_TIME_NAME', 'USE_TIME_NAME' );
define ( 'UPLOAD_DATE_SUB', 'USE_DATE_SUB' );
define ( 'UPLOAD_RAND_SUB', 'USE_RAND_SUB' );

class Helper_Upload {
	protected $allowExt = array ('jpg', 'gif', 'png', 'jpeg', 'rar', 'txt', 'zip', 'doc' );
	protected $maxSize = 10000000; //10M
	protected $pool_file = false;
	protected $pool_to = false;
	protected $pool_sub = false;
	protected $pool_path = false;
	protected $pool_error;
	protected $pool_log;
	
	public function sub($sub = "") {
		$this->fixSep ( $sub );
		switch ($sub) {
			case UPLOAD_DATE_SUB :
				$sub = md5 ( rand () );
				$this->pool_sub = $sub;
				break;
			case UPLOAD_RAND_SUB :
				$sub = md5 ( rand () );
				$this->pool_sub = $sub;
				break;
			default :
				$this->pool_sub = $sub;
		}
		return $this;
	}
	
	public function allow($str) {
		$this->allowExt = explode ( ',', $str );
		return $this;
	}
	
	public function max($size) {
		$size > 0 && $this->maxSize = ( int ) $size;
		return $this;
	}
	
	public function file($file) {
		$this->pool_file = $file;
		return $this;
	}
	
	public function to($to) {
		$this->fixSep ( $to );
		$this->pool_to = $to;
		return $this;
	}
	
	public function upload($nametype = UPLOAD_TIME_NAME) {
		if (! $this->pool_file || ! $this->pool_to) {
			return false;
		}
		if (substr ( $this->pool_to, - 1 ) != DIR_SEP)
			$this->pool_to .= DIR_SEP;
		$this->pool_path = $this->doUpload ( $this->pool_file, $this->pool_to . $this->pool_sub, $nametype );
		return $this;
	}
	
	public function getPath() {
		return $this->pool_path;
	}
	
	protected function doUpload($file, $topath, $nametype = UPLOAD_TIME_NAME) {
		$path = false;
		if (is_array ( $file ["name"] )) {
			$count = count ( $file ["name"] );
			for($i = 0; $i < $count; $i ++) {
				$nfile ['name'] = $file ['name'] [$i];
				$nfile ['type'] = $file ["type"] [$i];
				$nfile ['tmp_name'] = $file ["tmp_name"] [$i];
				$nfile ['error'] = $file ["error"] [$i];
				$nfile ['size'] = $file ["size"] [$i];
				$path [$i] = $this->doUpload ( $nfile, $topath, $nametype );
			}
		} else {
			$filename = $file ["name"];
			if (! $filename) {
				$this->setError ( " empty file! " );
				return false;
			}
			$fileext = $this->getFileExt ( $filename );
			if (! $fileext) {
				$this->setError ( $filename . " has not extentsion!" );
				return false;
			}
			$filesize = $file ["size"];
			if (! in_array ( $fileext, $this->allowExt )) {
				$this->setError ( $filename . " is not avaliable file type!" );
			} elseif ($filesize > $this->maxSize) {
				$this->setError ( $filename . " size is more than max size!" );
			} else {
				//$this->uploadFile ( $file ['tmp_name'], $fileext );
				$rand = rand ();
				$time = time ();
				switch ($nametype) {
					case UPLOAD_ORG_NAME :
						$nname = $filename;
						break;
					case UPLOAD_RAND_NAME :
						$nname = md5 ( $time . $rand ) . $fileext;
						break;
					case UPLOAD_TIME_NAME :
						$nname = date ( "YmdHis" ) . $rand . "." . $fileext;
						break;
				}
				if (file_exists ( $nname )) {
					$this->setError ( $nname . ' has been exists! orignal name: ' . $filename );
					return false;
				}
				if (substr ( $topath, - 1 ) != DIR_SEP)
					$topath .= DIR_SEP;
				if (! is_dir ( $topath ) && ! @mkdir ( $topath, 0777, true )) {
					$this->setError ( $topath . ' can\'t be created! ' );
					return false;
				}
				$path = $topath . $nname;
				if (@move_uploaded_file ( $file ['tmp_name'], $path )) {
					return $path;
				}
				$this->setError ( $filename . " can't be move to {$path}!" );
				return false;
			}
		}
		return $path;
	}
	
	public function getError() {
		return $this->pool_error;
	}
	
	public function getFailKey() {
	
	}
	
	protected function getFileExt($filename) {
		$pos = strrpos ( $filename, "." );
		if ($pos === false) {
			return false;
		}
		$ext_name = substr ( $filename, $pos + 1 );
		return strtolower ( $ext_name );
	}
	
	protected function setError($data) {
		$this->pool_error [] = $data;
	}
	
	protected function fixSep(&$str) {
		if ($str && strpos ( $str, '/' ) !== false) {
			$str = str_replace ( '/', DIR_SEP, $str );
		}
	}

}
?>
